Pause
Show Transcript
Video info
03:57
Pause
Seek 10 seconds backwards
Seek 10 seconds forward
00:11 / 05:54
Mute
Click to volume controlUse the arrows to control the volume
Enable Captions
Settings
Picture in picture
Fullscreen
More transcript options
Hide Transcript
Skip transcript
00:02
When you call a function,
00:04
you can pass variables to it,
00:06
and when it finishes,
00:08
it can return something back as well.
00:11
Let's look at how this is
00:12
done and how to make
00:14
sure we understand
00:15
the scope of these variables.
00:17
As a simple example,
00:18
let's create a function add
00:20
that will receive two numbers,
00:22
add them together, and return the result.
00:25
The first thing we need to
00:27
do is define our function.
00:29
So I type DEF to define it,
00:32
and then I need a name.
00:34
How about add for this?
00:36
Then, I need to add parameters.
00:39
And this is, what does
00:40
this function need in order to do its job?
00:43
In this case, it needs the two numbers.
00:46
I could choose any variable name that I want.
00:48
And so how, in this case,
00:50
I'll call it first and
00:52
second for the first and second number.
00:54
I finished the parentheses,
00:56
and now I put my colon and start indenting.
01:00
Now, I'll go ahead and
01:02
put my triple quotes and
01:03
add a dock string here
01:05
to describe what this does.
01:06
So this function adds two numbers
01:10
together and returns the result.
01:14
Great. Now I can go
01:16
ahead and put in the body of the function.
01:19
In this case, it's pretty simple.
01:21
The result is the first number
01:24
plus the second number.
01:26
Now, I want to send that result back to
01:29
the person who called this function
01:31
or the code that called this function.
01:33
And I do that by using the return keyword,
01:36
and then I choose the name of
01:38
the variable that I want to send back.
01:40
In this case, it's called result.
01:42
But there's nothing magic
01:44
about the choice of that name.
01:45
That just happens to be the
01:47
variable that I want to send back.
01:49
So, now that I have this function defined,
01:52
I can call it down below in my program here.
01:55
So, let's create a couple of variables.
01:57
X is three and y is four.
02:01
And then I can call this function.
02:03
Add, and I'll pass it x and y.
02:07
And then it will return things back,
02:09
and I'll save them into a new variable.
02:11
Maybe I'll call it total.
02:12
Notice, this is just like when
02:15
you called other functions
02:16
written by other people.
02:17
But in this case, it's one that
02:19
we've written, and that's okay.
02:20
So, now, let's run
02:22
this code and see what happens,
02:24
and maybe we'll print
02:25
the total out here at the end two.
02:26
Let's run this, and we'll do it with
02:28
the debugger so that we can walk through.
02:30
I'll set a break point at the beginning,
02:32
and now we can run this with debugging.
02:37
As we do this, notice at first,
02:40
nothing happens when we define the function.
02:43
It was just our way of telling
02:45
Python that this is now available.
02:48
I step over those two lines and
02:51
we see our variables x and y,
02:53
and we're going to watch these
02:54
closely as we jump back and forth.
02:56
I usually use step over,
02:59
and if I click Step over,
03:00
it will step over the call
03:02
to that function and give me the result back.
03:05
Notice here that I see the return value
03:09
of the function is coming back as seven,
03:11
and I can see that that variable total
03:14
now has the value seven in it,
03:16
and it will print that out.
03:18
It's done the work.
03:19
So let's do this one more time.
03:21
But this time, let's
03:23
step into that function call.
03:25
So when I get to line 11,
03:28
instead of clicking step over,
03:29
I'm going to click the down arrow
03:31
to step into the function.
03:34
Notice as I do this now,
03:37
I've jumped up to line five.
03:39
I'm in the function itself,
03:41
and I can see that
03:43
I have different variables now.
03:44
I'm no longer seeing x and y.
03:47
They're in a different scope.
03:49
Here I have my own set
03:50
of variables to work with,
03:51
called first and second,
03:53
but they have been populated
03:54
with the values that I scented.
03:57
I step over, I see the result now of seven.
04:00
And then when I click step over again,
04:02
it jumps back because the function is over.
04:05
So I jump back down to line ten and notice
04:08
down here that I'm back to
04:10
this set of variables,
04:12
the x and the y and the total,
04:14
because the function defined its own scope.
04:16
I can't use first and second down here.
04:19
It just doesn't work.
04:20
Those variables aren't available.
04:23
Instead, I can print
04:24
out the total and be done.
04:27
So let's update the program
04:29
slightly and call it one more time.
04:32
This time, instead of passing x and y,
04:34
maybe I want to pass y twice.
04:36
Can we do that? Sure.
04:38
It's just passing those values in.
04:40
And then we'll print the total out again.
04:43
So now let's go ahead and debug.
04:46
And I'll step over down here.
04:49
Now notice, I'm on line 11,
04:51
and when I step into the function,
04:53
I jump up and first and second,
04:56
have the values three and four.
04:58
When when I go back,
05:01
it will jump back to where it came from,
05:04
line 11 and now onto line 12.
05:07
Python keeps track of where it was
05:10
when it called the function
05:11
so that it knows how to go back.
05:13
Now, when I call it again, I'll step into it.
05:17
It jumps back up here again, but this time,
05:19
first and second have different values,
05:22
a four and a four.
05:24
It computes the result,
05:26
and now it jumps back to where it was before.
05:29
Now I'm down on line 15,
05:31
and it will print that total out.
05:34
The add function in this example
05:37
isn't very complicated.
05:39
But once you get comfortable with the idea of
05:41
passing values to functions
05:43
and getting values back,
05:45
you can write functions to
05:46
accomplish any task that you need.
Resume AutoScroll
Copy debug info